using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.ObjectBuilder;
using GC2.ExceptionHandler;
using GC2.ExceptionHandler.Services;
using GC2.Entities.Admin;
using GC2.DataAccess;

namespace GC2.DataAccess.DAAdmin
{
    public class DANotasCred
    {
        private Database _database;        // Objeto DataBase del Data Access Application Block

        #region Propiedades miembro
        /// <summary>
        /// Seteo la propiedad _database de la clase.
        /// </summary>
        private Database DataBase
        {
            get { return _database; }
            set { _database = value; }
        }
        #endregion

        #region Constructor
        /// <summary>
        /// Contructor de la clase sin parametro. Crea un objeto DataBase
        /// del Data AppBlock tomando el defaultDatabase del app.config.
        /// </summary>
        //public AplicacionesDataAccess()
        //{
        //    this.DataBase = DatabaseFactory.CreateDatabase();
        //}
        #endregion

        #region DANotasCredInsert
        /// <summary>
        /// Inserta Factura 
        /// </summary>
        /// <param name="Connection"></param>
        /// <param name="NotasCred"></param>
        /// <returns>NotasCred</returns>
        public void DANotasCredInsert(string Connection, NotasCred factura)
        {
            this.DataBase = DatabaseFactory.CreateDatabase(Connection);

            DbCommand cmd = DataBase.GetStoredProcCommand("NotasCred_Insert");

            try
            {
                using (cmd)
                {
                    DataBase.AddInParameter(cmd, "@Sucursal", DbType.Int16, factura.Sucursal);
                    DataBase.AddInParameter(cmd, "@Numero", DbType.Int32, factura.Numero);
                    DataBase.AddInParameter(cmd, "@Prefijo", DbType.String, factura.Prefijo);
                    DataBase.AddInParameter(cmd, "@Talonario", DbType.Int16, factura.Talonario);
                    DataBase.AddInParameter(cmd, "@Nombre", DbType.String, factura.Nombre);
                    DataBase.AddInParameter(cmd, "@Id_Venta", DbType.Int32, factura.Id_Venta);

                    DataBase.ExecuteNonQuery(cmd);
                }
            }
            catch (SqlException sql_ex)
            {
                ExceptionSQL exNew = new ExceptionSQL();
                throw new System.Exception(exNew.exSQL(sql_ex.ToString()).ToString());
            }
            catch (Exception ex)
            {
                ExceptionDAL exNew = new ExceptionDAL();
                throw new System.Exception(exNew.exDAL(ex.ToString()).ToString());
            }
        }
        #endregion

        #region DANotasCredGetOne
        /// <summary>
        /// Devuelve una Factura
        /// </summary>
        /// <param name="Connection"></param>
        /// <param name="NotasCred"></param>
        /// <returns>NotasCred</returns>
        public NotasCred DANotasCredGetOne(string Connection, NotasCred factura)
        {
            this.DataBase = DatabaseFactory.CreateDatabase(Connection);
            DbCommand cmd = DataBase.GetStoredProcCommand("NotasCred_GetOne");
            
            List<NotasCred> listNotasCred = new List<NotasCred>();
            try
            {
                using (cmd)
                {
                    DataBase.AddInParameter(cmd, "@Numero", DbType.Int16, factura.Numero);
                    //DataBase.AddInParameter(cmd, "OrdenDeRegistros", DbType.String, OrdenDeRegistros);
                    IDataReader Odbreader = DataBase.ExecuteReader(cmd);
                    while (Odbreader.Read())
                    {
                        NotasCred oFactura = new NotasCred();
                        oFactura.Numero = int.Parse(Odbreader["Numero"].ToString());
                        //oFactura.Talonario = int.Parse(Odbreader["Talonario"].ToString());
                        //oFactura.Nombre = Odbreader["Nombre"].ToString();
                        oFactura.Id_Venta = int.Parse(Odbreader["Id_Venta"].ToString());

                        listNotasCred.Add(oFactura);
                    }
                }
            }
            catch (SqlException sql_ex)
            {
                ExceptionSQL exNew = new ExceptionSQL();
                throw new System.Exception(exNew.exSQL(sql_ex.ToString()).ToString());
            }
            catch (Exception ex)
            {
                ExceptionDAL exNew = new ExceptionDAL();
                throw new System.Exception(exNew.exDAL(ex.ToString()).ToString());
            }
            if (listNotasCred.Count > 0)
            {
                return listNotasCred[0];
            }
            else
            {
                NotasCred facturaRet = new NotasCred();
                return facturaRet;
            }
        }
        #endregion

        #region DANotasCredProximoNumero
        /// <summary>
        /// Devuelve una Factura
        /// </summary>
        /// <param name="Connection"></param>
        /// <param name="NotasCred"></param>
        /// <returns>int</returns>
        public int DANotasCredProximoNumero(string Connection, NotasCred factura)
        {
            this.DataBase = DatabaseFactory.CreateDatabase(Connection);
            DbCommand cmd = DataBase.GetStoredProcCommand("ProximoNumero");
            int iProximo = 0;
            try
            {
                using (cmd)
                {
                    DataBase.AddInParameter(cmd, "@Tabla", DbType.String, "NotasCred");
                    DataBase.AddInParameter(cmd, "@Sucursal", DbType.String, factura.Sucursal.ToString());
                    DataBase.AddInParameter(cmd, "@Campo", DbType.String, "Talonario");
                    DataBase.AddInParameter(cmd, "@Valor", DbType.String, factura.Talonario.ToString());
                    
                    IDataReader Odbreader = DataBase.ExecuteReader(cmd);
                    while (Odbreader.Read())
                    {
                        iProximo = Convert.ToInt16(Odbreader["ProxNumero"].ToString());
                    }
                }
            }
            catch (SqlException sql_ex)
            {
                ExceptionSQL exNew = new ExceptionSQL();
                throw new System.Exception(exNew.exSQL(sql_ex.ToString()).ToString());
            }
            catch (Exception ex)
            {
                ExceptionDAL exNew = new ExceptionDAL();
                throw new System.Exception(exNew.exDAL(ex.ToString()).ToString());
            }
            return iProximo;
        }
        #endregion
    }
}

